package com.sitech.portal.webHooks;

import com.sitech.ibnms.core.BaseIbnmsAction;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.system.domain.TblSYS_ROLE;
import com.sitech.portal.system.service.ISysRoleService;
import com.sitech.portal.webHooks.domain.HookRequestBody;
import com.sitech.portal.webHooks.domain.Role;
import com.sitech.portal.webHooks.domain.WebHooks;
import com.sitech.portal.webHooks.service.IWebHooksService;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: yanran
 * Date: 15-9-11
 * Time: 上午10:19
 * To change this template use File | Settings | File Templates.
 */
public class RoleEventProcessor extends BaseIbnmsAction {
    private IWebHooksService webHooksService;
    private ISysRoleService sysRoleService;
    private static final Log logger = LogFactory.getLog(RoleEventProcessor.class);


    public Object whenAddRole(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        TblSYS_ROLE  role= (TblSYS_ROLE)joinPoint.getArgs()[0];
        Role afterRole = new Role(role);
        afterRole.setCREATED_BY(this.getCurrentUser().getLOGIN_ID());
        afterRole.setCREATED_AT(EventProcessorUtil.getNowTime());
        Role beforeRole = null;

        String object_id = role.getROLE_ID();
        String object_kind = "role";
        String operate_kind = "create";
        Object o = null;
        try {
            o = joinPoint.proceed();
            //调用接口
            List<WebHooks>  list= webHooksService.selectWebHooksUrl(object_kind);
            HookRequestBody hrb = new HookRequestBody();
            hrb.setObject_kind(object_kind);
            hrb.setOperate_kind(operate_kind);
            hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(beforeRole));
            hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(afterRole));
            hrb.setObject_id(object_id);

            for(WebHooks os : list){
                String url = os.getURL();
                PostMethod post = new PostMethod(url);
                 post.setRequestBody(JSONUtil.toJSON(hrb));
                try {
                    new HttpClient().executeMethod(post);
                }catch (Exception e){
                    logger.error(e);
                }
                logger.info("增加角色同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }
    public Object whenUpdateRole(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        TblSYS_ROLE after = (TblSYS_ROLE)joinPoint.getArgs()[0];
        TblSYS_ROLE before =  sysRoleService.findByPK(after.getROLE_ID());

        Role afterRole = new Role(after);
        afterRole.setUPDATED_BY(this.getCurrentUser().getLOGIN_ID());
        afterRole.setUPDATED_AT(EventProcessorUtil.getNowTime());
        Role beforeRole = new Role(before);

        String object_id = before.getROLE_ID();
        String object_kind = "role";
        String operate_kind = "update";
        Object o = null;
        try {
            o = joinPoint.proceed();
            //调用接口
            List<WebHooks>  list= webHooksService.selectWebHooksUrl(object_kind);
            HookRequestBody hrb = new HookRequestBody();
            hrb.setObject_kind(object_kind);
            hrb.setOperate_kind(operate_kind);
            hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(beforeRole));
            hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(afterRole));
            hrb.setObject_id(object_id);


            for(WebHooks os : list){
                String url = os.getURL();
                PostMethod post = new PostMethod(url);
                post.setRequestBody(JSONUtil.toJSON(hrb));
                try {
                    new HttpClient().executeMethod(post);
                }catch (Exception e){
                    logger.error(e);
                }
                logger.info("修改角色同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }

    public Object whenDeleteRole(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {

        String roleIds = (String)joinPoint.getArgs()[0];
        String[] array = roleIds.split(",");
        Object o = null;
        try {
            o = joinPoint.proceed();
            String object_kind = "role";
            String operate_kind = "delete";
            List<WebHooks>  urllist= webHooksService.selectWebHooksUrl(object_kind);
            for(String roleId : array){
                String object_id = roleId;

                Role roleBefore = null;
                Role roleAfter = new Role();
                roleAfter.setROLE_ID(object_id);
                roleAfter.setDELETED_BY(this.getCurrentUser().getLOGIN_ID());
                roleAfter.setDELETED_AT(EventProcessorUtil.getNowTime());

                HookRequestBody hrb = new HookRequestBody();
                hrb.setObject_kind(object_kind);
                hrb.setOperate_kind(operate_kind);
                hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(roleBefore));
                hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(roleAfter));
                hrb.setObject_id(object_id);
                for(WebHooks os : urllist){
                    String url = os.getURL();
                    PostMethod post = new PostMethod(url);
                    post.setRequestBody(JSONUtil.toJSON(hrb));
                    try {
                        new HttpClient().executeMethod(post);
                    }catch (Exception e){
                        logger.error(e);
                    }
                    logger.info("删除角色同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
                }
            }

        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }

    public void setWebHooksService(IWebHooksService webHooksService) {
        this.webHooksService = webHooksService;
    }

    public void setSysRoleService(ISysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }
}
